<?php

use Illuminate\Database\Seeder;
use Carbon\Carbon;

class AreasTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $data = file_get_contents(resource_path('areas.json'));
        $arr = json_decode($data,true);

        DB::table('areas')->truncate();

        $provinces = $arr[86];

        $now = Carbon::now()->toDateTimeString();
        foreach ($provinces as $k=>$v)
        {
            if(empty($arr[$k]))
            {
                continue;
            }
            $pid = DB::table('areas')->insertGetId([
                'type' => 1,
                'pid' => 0,
                'name' => $v,
                'created_at' => $now,
                'updated_at' => $now
            ]);


            $citys = $arr[$k];
            foreach ($citys as $kk=>$vv)
            {
                if(isset($arr[$kk]))
                {
                    $city_id = DB::table('areas')->insertGetId([
                        'type' => 2,
                        'pid' => $pid,
                        'name' => $vv,
                        'created_at' => $now,
                        'updated_at' => $now
                    ]);

                    $areas = $arr[$kk];

                    foreach ($areas as $kkk=>$vvv)
                    {
                        $area_id = DB::table('areas')->insertGetId([
                            'type' => 3,
                            'pid' => $city_id,
                            'name' => $vvv,
                            'created_at' => $now,
                            'updated_at' => $now
                        ]);
                    }
                }

            }
        }
    }

    public function down()
    {
        DB::table('areas')->truncate();
    }
}
